#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/select.h>
#include <ctype.h>

void sys_err(const char* str)
{
    perror(str);
    exit(1);
}

int main(int argc,char* argv[])
{
    int sockfd,ret,new_sockfd,maxfd,Ret;
    int i,j,n;
    socklen_t addr_len;
    char buf[BUFSIZ];
    char str[INET_ADDRSTRLEN];
    struct sockaddr_in server_addr,client_addr;
    //bzero(&server_addr,sizeof(server_addr));
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd < 0)
    {
        sys_err("socket()");
    }
    int opt = 1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
    ret = bind(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));
    if(ret < 0)
    {
        sys_err("bind()");
    }
    ret = listen(sockfd,10);
    if(ret < 0)
    {
        sys_err("listen()");
    }
    fd_set rset,allset;
    int client[FD_SETSIZE];
    int k = 0,maxi;
    maxi = -1;
    for(k = 0;k < FD_SETSIZE;k++)
    {
        client[k] = -1;
    }
    //memset(&client,-1,sizeof(client));
    FD_ZERO(&allset);
    FD_SET(sockfd,&allset);
    maxfd = sockfd;
    /*
    最初的版本
    while(1)
    {
        rset = allset;
        Ret = select(sockfd+1,&rset,NULL,NULL,NULL);
        if(Ret < 0)
        {
            sys_err("select()");
        }
        if(FD_ISSET(sockfd,&rset))
        {
            addr_len = sizeof(client_addr);
            new_sockfd = accept(sockfd,(struct sockaddr*)&client_addr,&addr_len);
            if(new_sockfd < 0)
            {
                sys_err("accept()");
            }
            FD_SET(new_sockfd,&allset);
            if(maxfd < new_sockfd)
            {
                maxfd = new_sockfd;
            }
            if(--Ret == 0)
            {
                continue;
            }
        }
        for(i = sockfd+1;i <= maxfd;i++)
        {
            if(FD_ISSET(i,&rset))
            {
                n = read(i,buf,sizeof(buf));
                if(n < 0)
                {
                    sys_err("read()");
                }
                else if(n == 0)
                {
                    close(i);
                    FD_CLR(i,&rset);
                }
                else
                {
                    for(j = 0;j < n;j++)
                    {
                        buf[j] = toupper(buf[i]);
                    }
                    ret = write(i,buf,n);
                    if(ret < 0)
                    {
                        sys_err("write()");
                    }
                }
            }
        }
    }
    */
   //用自定义数组提高效率
    while(1)
    {
        rset = allset;
        Ret = select(maxfd+1,&rset,NULL,NULL,NULL);
        if(Ret < 0)
        {
            sys_err("select()");
        }
        if(FD_ISSET(sockfd,&rset))
        {
            addr_len = sizeof(client_addr);
            new_sockfd = accept(sockfd,(struct sockaddr*)&client_addr,&addr_len);
            printf("reveived from %s at port %d\n",inet_ntop(AF_INET,&client_addr.sin_addr,str,sizeof(str)),ntohs(client_addr.sin_port));
            if(new_sockfd < 0)
            {
                sys_err("accept()");
            }
            for(i = 0;i < FD_SETSIZE;i++)
            {
                if(client[i] < 0)
                {
                    client[i] = new_sockfd;
                    break;
                }
            }
            if(i == FD_SETSIZE)
            {
                fputs("too many client\n",stderr);
                exit(1);
            }
            FD_SET(new_sockfd,&allset);
            if(maxfd < new_sockfd)
                maxfd = new_sockfd;

            if(maxi < i)
                maxi = i;

            if(--Ret == 0)
            {
                continue;
            }
        }
        for(i = 0;i <= maxi;i++)
        {
            if(client[i] < 0)
                continue;
            if(FD_ISSET(client[i],&rset))
            {
                n = read(client[i],buf,sizeof(buf));
                if(n < 0)
                {
                    sys_err("read()");
                }
                else if(n == 0)
                {
                    close(client[i]);
                    FD_CLR(client[i],&allset);
                    client[i] = -1;
                }
                else
                {
                    for(j = 0;j < n;j++)
                    {
                        buf[j] = toupper(buf[j]);
                    }
                    ret = write(client[i],buf,n);
                    if(ret < 0)
                    {
                        sys_err("write()");
                    }
                    ret = write(STDOUT_FILENO,buf,n);
                    if(ret < 0)
                    {
                        sys_err("write()");
                    }
                }
            }
        }
    }
    close(sockfd);
    return 0;
}
